package com.yanqu.road.server.manager.activity.commonteam.activitytypehandler.handler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.yanqu.road.entity.activity.ActivityConfig;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.commonteam.CommonTeamConfigData;
import com.yanqu.road.entity.activity.commonteam.CommonTeamInitUserData;
import com.yanqu.road.entity.activity.commonteam.data.CommonTeamUserData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiEggData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiPlotData;
import com.yanqu.road.entity.activity.sonhai.data.SonHaiRegionData;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.entity.player.CrossUserBaseInfo;
import com.yanqu.road.exception.BusinessException;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.CommonTeamProto;
import com.yanqu.road.pb.activity.SonHaiProto;
import com.yanqu.road.server.manager.activity.commonteam.CommonTeamActivity;
import com.yanqu.road.server.manager.activity.commonteam.activitytypehandler.CommonTeamActivityTypeStrategy;
import com.yanqu.road.server.manager.activity.doublespring.CrossDoubleSpringActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivity;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiActivityMgr;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiTeam;
import com.yanqu.road.server.manager.activity.sonhai.CrossSonHaiUser;
import com.yanqu.road.server.manager.activity.sonhai.pb.SonHaiActivityPb;
import com.yanqu.road.server.manager.tomb.event.Buff;
import com.yanqu.road.server.manager.user.CrossUserMgr;
import com.yanqu.road.server.manger.ServerLanguageMgr;
import com.yanqu.road.utils.FixSizeLinkedList;
import com.yanqu.road.utils.date.DateHelper;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 山海伏兽策略
 */
public class CommonTeamSonHaiStrategy implements CommonTeamActivityTypeStrategy {

    /**
     * 日志
     */
    private static Logger logger = LogManager.getLogger(CommonTeamSonHaiStrategy.class.getName());

    /**
     * 获取玩家活动积分
     * @param activityId
     * @param groupId
     * @param userId
     * @return
     */
    @Override
    public List<Long> getUserActivityScoreList(int activityId, int groupId, long userId) {
        List<Long> scoreList = new ArrayList<>();
        //获取山海活动
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(activityId, groupId);
        if (activity == null) {
            scoreList.add(0L);
            scoreList.add(0L);
            return scoreList;
        }
        //获取山海玩家
        CrossSonHaiUser user = activity.getUser(userId);
        if (user == null) {
            scoreList.add(0L);
            scoreList.add(0L);
            return scoreList;
        }
        //山海伏兽积分
        scoreList.add(user.getScore());
        scoreList.add(user.getAuraScore());
        return scoreList;
    }

    /**
     * 获取队伍榜单类型
     * @return
     */
    @Override
    public List<Integer> getTeamRankConditionTypes() {
        List<Integer> types = new ArrayList<>();
        types.add(eGamePlayerEventType.SonHaiTeamRank.getValue());
        return types;
    }

    /**
     * 格式化聊天消息
     * @param activityId
     * @param groupId
     * @param chatMsgList
     * @return
     */
    @Override
    public FixSizeLinkedList<CommonTeamProto.CommonTeamChatMsg> formatTeamChatMsgList(int activityId, int groupId, FixSizeLinkedList<CommonTeamProto.CommonTeamChatMsg> chatMsgList) {
        //获取山海活动
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(activityId, groupId);
        if (activity == null) {
            return chatMsgList;
        }
        FixSizeLinkedList<CommonTeamProto.CommonTeamChatMsg> newList =  new FixSizeLinkedList<>(30);
        for (CommonTeamProto.CommonTeamChatMsg chatMsg : chatMsgList) {
            if (chatMsg.hasPlotData()) {
                SonHaiProto.SonHaiChatMsgPlotData plotData = chatMsg.getPlotData();
                //获取玩家
                CrossSonHaiUser user = activity.getUser(plotData.getUserId());
                if (user == null) {
                    continue;
                }
                //获取队伍
                CrossSonHaiTeam team = activity.getTeam(user.getTeamId());
                if (team == null) {
                    continue;
                }
                //获取区域
                SonHaiRegionData regionData = team.getRegion(plotData.getRegionId());
                if (regionData == null) {
                    //区域已经不存在，移除消息
                    continue;
                }
                //获取地块
                SonHaiPlotData plot = regionData.getPlotData(plotData.getX(), plotData.getY());
                if (plot == null) {
                    //地块已经不存在，移除消息
                    continue;
                }
                //不同区域
                if (plotData.getRegionUniqueId() != plot.getRegionUniqueId()) {
                    continue;
                }
                //构造地块PB
                SonHaiProto.SonHaiChatMsgPlotData.Builder newPlotData = plotData.toBuilder();
                newPlotData.setWinFlag(plot.getWinFlag());
                //如果是pvp地块，获取下pvp玩家数据
                if (plot.getPvpUserId() > 0) {
                    CrossUserBaseInfo targetUserBase = CrossUserMgr.getCrossUserBaseInfo(plot.getPvpUserId());
                    if (targetUserBase != null && targetUserBase.getUserBaseInfo() != null) {
                        newPlotData.setPvpUserInfo(PlayerBasePb.parsePlayerBaseTempMsg(targetUserBase.getUserBaseInfo()));
                    }
                }
                //如果是蛋事件，获取蛋信息
                if (plot.getEggId() > 0) {
                    SonHaiEggData eggData = activity.getEgg(plot.getEggId());
                    if (eggData != null) {
                        newPlotData.setEggName(ServerLanguageMgr.getContent(eggData.getEggName(), user.getLanguage()));
                    }
                }
                //构造消息PB
                CommonTeamProto.CommonTeamChatMsg.Builder newChatMsg = CommonTeamProto.CommonTeamChatMsg.newBuilder();
                newChatMsg.setMsg(chatMsg.getMsg());
                newChatMsg.setPlotData(newPlotData);
                newList.add(newChatMsg.build());
            } else {
                newList.add(chatMsg);
            }
        }
        return newList;
    }

    /**
     * 同意入队后处理
     * @param activityId
     * @param groupId
     * @param teamId
     * @param userData
     * @throws BusinessException
     */
    @Override
    public void agreeJoinTeamAfterHandle(int activityId, int groupId, int teamId, CommonTeamUserData userData, String language) throws BusinessException {
        //获取山海活动
        CrossSonHaiActivity activity = CrossSonHaiActivityMgr.getActivity(activityId, groupId);
        if (activity == null) {
            return;
        }
        //判断队伍是否存在
        CrossSonHaiTeam team = activity.getTeam(teamId);
        if (team == null) {
            return;//队伍不存在已经存在
        }
        //判断玩家是否存在
        CrossSonHaiUser user = activity.getUser(userData.getUserId());
        if (user != null) {
            return;//玩家已经存在
        }
        //初始化玩家
        activity.initUser(userData.getUserId(), userData.getServerId(), team, userData.getScore(), language);
    }
}
